package com.fofia.iotweb.jobs;

import com.fofia.iotweb.common.IoHelper;
import com.fofia.iotweb.web.domain.dto.RuminateTagDto;

import java.util.ArrayList;
import java.util.List;

/**
 * 反刍标签原始解析
 */
public class RuminateHelper {

    /**
     *反刍标签原始数据解析
     * @param hex 原始数据hex
     * @return
     */
    public static List<RuminateTagDto> parse(String hex){
        List<RuminateTagDto> data = new ArrayList<>();
        if (hex.length() == 64)
        {
            data = parseV1(hex);
        }
        if (hex.length() == 66)
        {
            data = parseV2(hex);
        }
        return data;
    }

    /**
     * 最老版本，基本上淘汰了 为了兼容还是保留一下
     * @param hex 32字节
     * @return
     */
    public static List<RuminateTagDto> parseV1(String hex){
        List<RuminateTagDto> data = new ArrayList<>();

        try{
            byte[] acc_buf = IoHelper.toHex(hex);

            //一共32个字节，每4个字节为一条记录包含一个x,y,z轴，共包含8条记录 62.5毫秒
            short t = 0;
            for (int i = 0; i < 8; i++) {
                short x = 0;
                x = (short) (0xff & (acc_buf[i * 4] << 6));
                t = (short) (acc_buf[i * 4] >> 2 | (0xff & ((acc_buf[i * 4 + 1] & 0x03) << 6)));
                x |= (short) (t << 8);
                x /= 64;

                short y = 0;
                y = (short) ((acc_buf[i * 4 + 1] & 0x0c) << 4);
                t = (short) (acc_buf[i * 4 + 1] >> 4);
                t |= (short) ((acc_buf[i * 4 + 2] & 0x0f) << 4);
                y |= (short) (t << 8);
                y /= 64;


                short z = 0;
                z = (short) ((acc_buf[i * 4 + 2] & 0x30) << 2);
                t = (short) ((acc_buf[i * 4 + 2] & 0xc0) >> 6);
                t |= (short) ((acc_buf[i * 4 + 3] & 0x3f) << 2);
                z |= (short) (t << 8);
                z /= 64;

                var model = new RuminateTagDto();
                model.setX(x);
                model.setY(y);
                model.setZ(z);
                //model.time = stime.AddMilliseconds(62).ToString("yyyy-MM-dd HH:mm:ss.fff");

                data.add(model);
            }
        }catch (Exception e){
            return null;
        }


        return data;
    }

    /**
     * 新格式了
     * @param hex 33字节
     * @return
     */
    public static List<RuminateTagDto> parseV2(String hex){
        List<RuminateTagDto> data = new ArrayList<>();

        try{
            byte[] buffer = IoHelper.toHex(hex);

            for (int i = 0; i < 8; i++)
            {
                short[] acc = new short[3];
                acc[0]=0;
                acc[1]=0;
                acc[2]=0;
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 11; k++)
                    {
                        if ((buffer[j * 11 + k] & (1 << i)) != 0)
                            acc[j] |= (short)(1 << k);
                    }
                    acc[j] <<= 5;
                    acc[j] >>= 5;
                }
                var model = new RuminateTagDto();
                model.setX(acc[0]);
                model.setY(acc[1]);
                model.setZ(acc[2]);
                data.add(model);
            }
        }catch (Exception ex){

        }

        return data;
    }
}
